// pages/classHomePage/signIn/index/index.js
const app = getApp()

Page({
  data: {
    signList: [],
    is_signed_list: [],
    signed_time_list: [],
    people: null,
    card: [{
      title: {
        text: "PK",
        color: "#272635" //字体颜色
      },
      tag: {
        text: "1小时前"
      },
      header: {
        bgcolor: "#f5f5f5", //#f5f5f5  #fbde35
        line: true
      }
    }],
    funList: {
      'funitems': [{
          "name": "PK",
          "url": "/pages/classHomePage/pk/index/index",
          "pic_url": "/assets/wx_pic/menu_pk@3x.png"
        },
        {
          "name": "盲盒",
          "url": "/pages/classHomePage/blindbox/index/index",
          "pic_url": "/assets/wx_pic/menu_mh@3x.png"
        },
        {
          "name": "签到",
          "url": "/pages/classHomePage/signIn/index/index",
          "pic_url": "/assets/wx_pic/menu_qd@3x.png"
        },
        {
          "name": "辩论会",
          "url": "/pages/classHomePage/debate/index/index",
          "pic_url": "/assets/wx_pic/menu_blh@3x.png"
        },
        {
          "name": "抢答",
          "url": "/pages/classHomePage/qiangda/index/index",
          "pic_url": "/assets/wx_pic/menu_qd_1@3x.png"
        },
        {
          "name": "讨论",
          "url": "/pages/classHomePage/discuss/index/index",
          "pic_url": "/assets/wx_pic/menu_tl@3x.png"
        },
        {
          "name": "成员分组",
          "url": "/pages/classHomePage/group/index/index",
          "pic_url": "/assets/wx_pic/menu_zyfz@3x.png"
        },
        {
          "name": "积分",
          "url": "/pages/classHomePage/integral/index/index",
          "pic_url": "/assets/wx_pic/menu_jf@3x.png"
        },
      ]
    },
  },

  onLoad(options) {
    let _this = this
    return new Promise(function (resolve, reject) {
      _this.setData({
        people: wx.getStorageSync("people")
      })
      //老师
      if (wx.getStorageSync("people") == 'teacher') {
        app.formGet('/get_all_qiandao_list?teacher_class_id=' + app.globalData.banji_info['id']).then(res => {
          console.log('老师获取签到列表：', res);
          _this.setData({
            signList: res
          });
          let title = {}
          let tag = {}
          let new_is_signed_list = []
          let new_signed_time_list = []
          for (let index = 0; index < _this.data.signList.length; index++) {
            tag = {
              'text': res[index]['createTime'].slice(0, 10) + '-' + res[index]['createTime'].slice(11, 16)
            }
            if (res[index]['isValid'] == 0) {
              //已经结束 
              title = {
                'text': '签到已结束',
                'color': null,
                'size': null
              }
            } else if (res[index]['isValid'] == 1) {
              //未结束 
              title = {
                'text': '签到进行中',
                'color': "#46be6c",
                'size': null
              }
            }
            new_is_signed_list.push(title)
            new_signed_time_list.push(tag)
            _this.setData({
              signed_time_list: new_signed_time_list,
              is_signed_list: new_is_signed_list
            })
            // console.log('signed_time_list', _this.data.signed_time_list);
            // console.log('is_signed_list', _this.data.is_signed_list);
          }
        })
      }
      //学生
      if (wx.getStorageSync("people") == 'student') {
        app.formPost('/get_all_class_by_student_id?student_id=' + app.globalData.student_info['id'], {}).then(res => {
          // console.log(res);
          let course_name = res[0]['className']
          app.formGet('/get_class_id_by_name?course_name=' + course_name).then(res => {
            // console.log(res);
            let teacher_class_id = res['message']
            app.formGet('/get_all_qiandao_list?teacher_class_id=' + teacher_class_id).then(res => {
              console.log('学生获取签到列表：', res);
              _this.setData({
                signList: res
              });
              resolve(res)
            })
          })
        })
      }
    }).then(res => {
      let sign_info = res
      let new_is_signed_list = []
      let new_signed_time_list = []
      for (let index = 0; index < _this.data.signList.length; index++) {
        _this.get_qiandao_state(res[index]['id']).then(res => {
          // console.log(index, res);
          let title = {}
          let tag = {}
          //isValid == 0：已经结束; code == 0: 未签到; code == 1: 已签到 
          if (sign_info[index]['isValid'] == 0 && res['code'] == 1) {
            //已经结束 已经签到
            title = {
              'text': '已结束 ' + res['message'],
              'color': null,
              'size': null
            }
          } else if (sign_info[index]['isValid'] == 0 && res['code'] == 0) {
            //已经结束 未签到
            title = {
              'text': '已结束 未签到',
              'color': "#FF0000",
              'size': null
            }
          } else if (sign_info[index]['isValid'] == 1 && res['code'] == 0) {
            //未结束 未签到
            title = {
              'text': res['message'],
              'color': "#46be6c",
              'size': null
            }
          } else if (sign_info[index]['isValid'] == 1 && res['code'] == 1) {
            //未结束 已签到
            title = {
              'text': res['message'],
              'color': "#000000",
              'size': null
            }
          }
          tag = {
            'text': sign_info[index]['createTime'].slice(0, 10) + '-' + sign_info[index]['createTime'].slice(11, 16)
          }
          new_is_signed_list.push(title)
          new_signed_time_list.push(tag)
          // console.log(new_signed_time_list);
          _this.setData({
            is_signed_list: new_is_signed_list,
            signed_time_list: new_signed_time_list
          })
        })
      }
    })

  },

  creatSign: function () {
    let _this = this
    wx.showModal({
      title: '输入本次签到名称',
      editable: true, //显示输入框
      cancelText: '取消',
      confirmText: '创建',
      success: res => {
        if (res.confirm) { //点击了确认
          console.log('正在创建签到：', res.content);
          let data = {
            'sign_in_title': res.content,
            'teacher_account': wx.getStorageSync("account"),
            'teacher_class_id': app.globalData.banji_info['id'],
          }
          app.formPost('/create_qiandao', data).then(res => {
            console.log('老师创建签到结果:', res);
            _this.onLoad()
            // wx.navigateBack()
          })
        }
      }
    })
    // wx.navigateTo({
    //   url: '/pages/classHomePage/signIn/createSign/createSign',
    // })
  },

  //选择签到item
  clickitem: function (e) {
    if (wx.getStorageSync('people') == 'teacher') {
      console.log('e.currentTarget.dataset', e.currentTarget.dataset);
      // console.log(e.currentTarget.dataset.title, e.currentTarget.dataset.signinid);
      wx.navigateTo({
        url: '../signMessage/signMessage?sign_in_title=' + e.currentTarget.dataset.title + '&teacher_sign_in_id=' + e.currentTarget.dataset.signinid + '&state=' + e.currentTarget.dataset['state'],
      })
    }

  },

  get_qiandao_state: function (teacher_sign_in_id) {
    return new Promise(function (resolve, reject) {
      let _this = this
      app.formGet('/get_qiandao_state?student_id=' + app.globalData.student_info['id'] + '&teacher_sign_in_id=' + teacher_sign_in_id).then(res => {
        // console.log('学生获取签到状态:', res);
        resolve(res)
      })
    })

  },

  button_sign: function (e) {
    console.log(e);
    let _this = this
    let data = {
      'code': wx.getStorageSync('myCode'),
      'sign_in_title': e.target.dataset['signintitle'],
      'student_account': app.globalData.student_info['account'],
      'student_id': app.globalData.student_info['id']
    }
    app.formPost('/add_qiandao', data).then(res => {
      console.log('签到情况：', res['message']);
      _this.onLoad()
    })
  },

  click_item: function (e) {
    if (e.currentTarget.dataset.url != '') {
      wx.navigateBack()
      wx.navigateTo({
        url: e.currentTarget.dataset.url,
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.onLoad()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})